
	
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import javax.swing.event.*;

/* This is nicer code than the version I showed in class.  The bar
 * is defined on the y-axis over the intervals 1 to 2, 2 to 3, and 3 to 4.
 * The viewer is at (10, 3, 0) looking at (0, 3, 0) with an orthogonal
 * projection.
 */

public class Bar implements GLEventListener, ActionListener, ChangeListener {
	
	public static void main(String[] args) {
		new Bar();
	}
	
	private int INTITIAL_WIDTH=1000;
	private int INITIAL_HEIGHT=1000;
	
	JButton quitButton;
	private GLCanvas canvas;
	private GL2 gl;
	private GLU glu;
	private JSlider redSlider, greenSlider, blueSlider;
	
	// The red bar extends from 1 to 2, the green bar from 2 to 3, and the blue bar from 3 to 4
	// along hte x-axis.
	
	float redBar[][] = new float [][] {
			{0,1,0}, {0, 2, 0}, {0, 2, 0.1f}, {0, 1, 0.1f}
	};
	float greenBar[][] = new float [][] {
			{0,2,0}, {0, 3, 0}, {0, 3, 0.1f}, {0, 2, 0.1f}
	};
	
	float blueBar[][] = new float [][] {
			{0,3,0}, {0, 4, 0}, {0, 4, 0.1f}, {0, 3, 0.1f}

	};
	
	// The viewer is at (2, 0, 0); this doesn't change.
	private float X = 2f;
	private float Y  = 0f;
	private float Z = 6f;
	
	// The following are the angles of the bar joints, controlled by the sliders.
	private float redTheta = 0;
	private float greenTheta = 0;
	private float blueTheta = 0;
	
	public  Bar() {
			GLProfile glp=GLProfile.getDefault();
			GLCapabilities caps = new GLCapabilities(glp);
			canvas = new GLCanvas(caps);
			JFrame frame = new JFrame("BAR");

			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
			frame.setSize(800, 800);
			frame.setLayout(new BorderLayout());
			JPanel north = new JPanel( new BorderLayout());
			JPanel topRow = new JPanel( new GridLayout(1, 8));
			
			quitButton = new JButton( "Quit");
			quitButton.addActionListener(this);
			topRow.add(quitButton);
			for (int i= 0; i < 7; i++)
				topRow.add(new JLabel( " ")); 
			
			north.add(topRow, BorderLayout.NORTH);
			
			JPanel south = new JPanel(new GridLayout(3, 1));
			JPanel secondRow = new JPanel(new BorderLayout());
			JPanel thirdRow = new JPanel( new BorderLayout());
			JPanel fourthRow = new JPanel( new BorderLayout() );
			redSlider = new JSlider(-90, 90);
			redSlider.setValue((int)(redTheta));
			redSlider.setMajorTickSpacing(45);
			redSlider.setPaintTicks(true);
			redSlider.setPaintLabels(true);
			redSlider.addChangeListener(this);
			JLabel redSliderName = new JLabel("   Red");
			secondRow.add(redSliderName, BorderLayout.WEST);
			secondRow.add(redSlider, BorderLayout.CENTER);
			south.add(secondRow);
			greenSlider = new JSlider(-90, 90);
			greenSlider.setValue((int)(greenTheta));
			greenSlider.setMajorTickSpacing(45);
			greenSlider.setPaintTicks(true);
			greenSlider.setPaintLabels(true);
			greenSlider.addChangeListener(this);
			JLabel greenSliderName = new JLabel(" Green");
			thirdRow.add(greenSliderName, BorderLayout.WEST);
			thirdRow.add(greenSlider, BorderLayout.CENTER);
			south.add(thirdRow);
			blueSlider = new JSlider(-90, 90);
			blueSlider.setValue((int)(blueTheta));
			blueSlider.setMajorTickSpacing(45);
			blueSlider.setPaintTicks(true);
			blueSlider.setPaintLabels(true);
			blueSlider.addChangeListener(this);
			JLabel blueSliderName = new JLabel("  Blue");
			fourthRow.add(blueSliderName, BorderLayout.WEST);
			fourthRow.add(blueSlider, BorderLayout.CENTER);
			south.add(fourthRow);
			

			north.add(south, BorderLayout.SOUTH);
			frame.add(north, BorderLayout.NORTH);
			JPanel myCanvas = new JPanel(new GridLayout(1,1));
			
			myCanvas.add(canvas);

			frame.add(myCanvas, BorderLayout.CENTER);
			
			frame.setVisible(true);
			canvas.addGLEventListener(this);

			FPSAnimator animator = new FPSAnimator(canvas, 60);
			animator.start(); 
				

	}
	public void actionPerformed(ActionEvent event) {
			if (event.getSource() == quitButton)
				System.exit(0);

	}
	
	public void stateChanged(ChangeEvent e) {
		if (e.getSource() == redSlider) {
			redTheta = redSlider.getValue();
		}
		else if (e.getSource() == greenSlider) {
			greenTheta = greenSlider.getValue();
		}
		else if (e.getSource() == blueSlider) {
			blueTheta = blueSlider.getValue();
		}

	}


	public void display(GLAutoDrawable drawable) {
		update();
		render();
	}

	private void update() {			
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glLoadIdentity();
		glu.gluLookAt(10, 3, 0, 0, 3, 0, 0, 0, 1);
	}

	
	private void render() {

		gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

		gl.glColor3f( 1, 0, 0);
		gl.glTranslatef(0, 1, 0.05f);
		gl.glRotatef(redTheta, 1, 0, 0);
		gl.glTranslatef(0, -1, -0.05f);
		polygon(redBar);
		
		gl.glColor3f( 0, 1, 0);
		gl.glTranslatef(0, 2, 0.05f);
		gl.glRotatef(greenTheta, 1, 0, 0);
		gl.glTranslatef(0, -2, -0.05f);
		polygon(greenBar);
		
		gl.glColor3f( 0, 0, 1);
		gl.glTranslatef(0, 3, 0.05f);
		gl.glRotatef(blueTheta, 1, 0, 0);
		gl.glTranslatef(0, -3, -0.05f);
		polygon(blueBar);
		
	}
	

	private void polygon(float vertices[][]) {
		gl.glBegin(GL2.GL_POLYGON);
			gl.glVertex3fv(vertices[0], 0);
			gl.glVertex3fv(vertices[1], 0);
			gl.glVertex3fv(vertices[2], 0);
			gl.glVertex3fv(vertices[3], 0);
		gl.glEnd();
			
	}
	
	public void dispose(GLAutoDrawable drawable) {
		// put the cleanup code here
		
	}

	public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL().getGL2();
		glu = new GLU();
		gl.glEnable(GL2.GL_DEPTH_TEST);
		}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		// this is called when the window is resized
		gl.glViewport(0, 0, width, height);
		float aspect = width*1.0f/height;
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrtho(-4, 3, -4, 3, 1, 200);
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glLoadIdentity();
		glu.gluLookAt(10, 3, 0, 0, 3, 0, 0, 0, 1);
		gl.glClearColor(1,  1, 1, 1);
	}
	
}
